package com.google.gdata.data.douban;

import java.io.IOException;
import java.util.List;

import org.xml.sax.Attributes;

import com.google.gdata.data.BaseEntry;

import com.google.gdata.data.ExtensionDescription;
import com.google.gdata.data.ExtensionProfile; 
import com.google.gdata.data.Person;
import com.google.gdata.data.TextConstruct;
import com.google.gdata.data.BaseEntry.AtomHandler; 
import com.google.gdata.util.ParseException;
import com.google.gdata.util.XmlParser;

@ExtensionDescription.Default(
		nsAlias = "", 
		nsUri = "http://www.w3.org/2005/Atom", 
		localName = "entry")
public class DoumailEntry extends BaseEntry<DoumailEntry> {

	public DoumailEntry() {
		super();
	}

	public DoumailEntry(BaseEntry sourceEntry) {
		super(sourceEntry);
	}

	@Override
	public void declareExtensions(ExtensionProfile extProfile) {

		super.declareExtensions(extProfile);

		extProfile.declare(DoumailEntry.class, Attribute.class); 
		extProfile.declare(DoumailEntry.class, Entity.class); 
		extProfile.declare(DoumailEntry.class,
		         Author.getDefaultDescription(false, true));
		new Author().declareExtensions(extProfile);
		extProfile.declareArbitraryXmlExtension(DoumailEntry.class);
	}
	
	protected List<Attribute> attributes;
	protected List<Entity> entities;

	public List<Attribute> getAttributes() {
		return getRepeatingExtension(Attribute.class);
	}
	public void setAttributes(List<Attribute> atts) {
		if (atts == null) {
			removeExtension(Attribute.class);
		} else {
			for (Attribute att : atts)
				addRepeatingExtension(att);
		}
	}
	
	public Author getReceiver(){
		List<Entity> es = this.getEntities();
		if(es!= null && es.size()>0){
			for(Entity e: es){ 
				if(e.getEntityName().equalsIgnoreCase("receiver")){
					Author author = new Author();
					author.setName(e.getName().getPlainText());
					author.setLinks(e.getLinks());
					author.setUri(e.getUri().getPlainText());
					return author;
				}
			}
			 
		}
		return null;
	}
	
	public void setReceiver(Person p){
		if(this.getReceiver() != null)
		{
			return;
		}
		Entity entity = new Entity();
		entity.setEntityName("receiver"); 
		TextConstruct t = TextConstruct.create(TextConstruct.Type.TEXT, p.getUri(), null);
		entity.setUri(t);
		addRepeatingExtension(entity); 
	}
	
	
	public List<Entity> getEntities() {
		return getRepeatingExtension(Entity.class);
		
	}
	public void setEntities(List<Entity> atts) {
		if (atts == null) {
			removeExtension(Entity.class);
		} else {
			for (Entity att : atts)
				addRepeatingExtension(att);
		}
	}
	
	public boolean getUnread(){
		List<Attribute>as = getAttributes();
		if(as!=null && as.size()>0){
			for(Attribute a : as ){
				if(a.getName().equals("unread")){
					return a.getContent().equalsIgnoreCase("true");
				}
			}
		}
		return false;
	
	}
	
 
	
	
	
	

}
